package control;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.util.Enumerator;
import org.apache.commons.collections.EnumerationUtils;

import model.DAOCurso;
import model.Curso;
import model.DAOProfessor;
import model.Professor;
import model.DAOTurma;
import model.Turma;
import model.Turno;

/**
 * Servlet implementation class CadastrarTurmaServlet
 */
public class CadastrarTurmaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastrarTurmaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int capacidade = Integer.parseInt(request.getParameter("cap"));
		String turno = request.getParameter("turno");
		String dataini = request.getParameter("datainicio");
		String datafim = request.getParameter("datafim");
		int id_prof = Integer.parseInt(request.getParameter("prof"));
		int id_curso = Integer.parseInt(request.getParameter("curso"));
		DAOProfessor daoProf = new DAOProfessor();
		DAOCurso daoCu = new DAOCurso();
		Professor p = daoProf.find(id_prof);
		Curso c = daoCu.find(id_curso);
		Turma t = new Turma();
		t.setCurso(c);
		t.setProfessor(p);
		t.setCapacidade(capacidade);
		t.setTurno(Turno.valueOf(turno));
		Calendar cali = Calendar.getInstance();
		Calendar calf = Calendar.getInstance();
		try 
			{
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				cali.setTime(sdf.parse(dataini));
				calf.setTime(sdf.parse(datafim));
			
			} 
		catch (ParseException e) 
			{
				e.printStackTrace();
			}
		t.setDataInicio(cali);
		t.setDataFim(calf);
		DAOTurma daoTu = new DAOTurma();
		daoTu.begin();
		daoTu.persist(t);
		daoTu.commit();
		String msg = new String ("Turma cadastrada!");
		request.setAttribute("msg", msg);
		RequestDispatcher rqd = request.getRequestDispatcher("painel.jsp");
		rqd.forward(request, response);
		
	}

}
